package com.sykj.why.controller.admin.successor;

import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.dto.PageDTO;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.SuccessorTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Eleven
 * @date 2019/7/11
 * @desc 后台传承人管理控制访问层
 */

@RestController
@RequestMapping("/admin/successor")
@Slf4j
@Api(value = "/admin/successor",description = "后台传承人管理控制访问层")
public class SuccessorController {

    @Autowired
    private SuccessorTService successorTService;

    /**
     * 分页查询传承人
     * @param pageDTO
     * @return
     */
    @RequestMapping(value = "/findByPage",method = RequestMethod.POST)
    @ApiOperation(value = "/findByPage",notes = "分页查询传承人")
    public ResponseEntity findByPage(@RequestBody PageDTO pageDTO){
        try {
            return Response.success("查询成功", successorTService.findByPage(pageDTO));
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("分页查询传承人出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 查询所有待审核传承人
     *
     * @param pageDTO
     * @return
     */
    @RequestMapping(value = "/findCheckByPage",method = RequestMethod.POST)
    @ApiOperation(value = "/findCheckByPage", notes = "查询所有待审核传承人")
    public ResponseEntity findCheckByPage(PageDTO pageDTO) {
        try {
            return Response.success("查询成功", successorTService.findCheckByPage(pageDTO));
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("查询所有待审核传承人出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 审核传承人
     *
     * @param id
     * @param flag
     * @return
     */
    @RequestMapping(value = "/check",method = RequestMethod.POST)
    @ApiOperation(value = "/check", notes = "审核传承人")
    public ResponseEntity check(@RequestParam String id, @RequestParam boolean flag, String checkInfo) {
        try {
            successorTService.check(id, flag, checkInfo);
            return Response.success("操作成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("审核传承人出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

}
